//
//  TibiAddressViewController.m
//  project
//
//  Created by 张亚林 on 2017/11/8.
//  Copyright © 2017年 Macmini. All rights reserved.
//

#import "TibiAddressViewController.h"

#import "TibiAddressTableViewCell.h"
#import "ZWTools.h"
#import "NSObject+MJExtension.h"
#import "AddCoinAddressViewController.h"

@interface TibiAddressViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tibiAddresstableview;
@property (nonatomic, strong)NSMutableArray *list; //数据源

@end

@implementation TibiAddressViewController

- (NSMutableArray *)list {
    
    if (!_list) {
        self.list = @[].mutableCopy;
    }
    return _list;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"BTC";
    self.tibiAddresstableview.delegate = self;
    self.tibiAddresstableview.dataSource = self;
    
    self.tibiAddresstableview.tableFooterView = [UIView new];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:YES];
    [self getCoinAddressList];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:YES];
    [self.list removeAllObjects];
}

#pragma mark - 获取收币地址
- (void)getCoinAddressList {
    
    NSMutableDictionary *requestParameter = [NSMutableDictionary dictionary];
    requestParameter[@"conllectionType"] = @"3";
    
    ZWTools *tools = [ZWTools sharedManager];
    [tools getCoinAddressList:requestParameter sucessBlock:^(id jsonResponse) {
        
        NSDictionary *responseDic = (NSDictionary *)jsonResponse;
        NSInteger result = [[responseDic objectForKey:@"code"]integerValue];
        if (result==0) {
            
            NSArray *array = jsonResponse[@"data"];
            for (NSDictionary *dict in array) {
            //    CoinModel *model = [CoinModel parse:dict];
                [self.list addObject:[CoinModel parse:dict]];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tibiAddresstableview reloadData];
            });
            
        } else if (result==100) {
            
            
        }
        
    } Error:^(id jsonResponse) {
        
    }];
}

- (IBAction)clickCoinAddress:(id)sender {
    
    AddCoinAddressViewController *addCoinAddressVC = [AddCoinAddressViewController new];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:addCoinAddressVC animated:YES];
//    self.hidesBottomBarWhenPushed = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.list.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 149;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TibiAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tibiAddressTableViewCell"];
    if (!cell) {
        cell =[[NSBundle mainBundle] loadNibNamed:@"TibiAddressTableViewCell" owner:nil options:nil].lastObject;
    }
    CoinModel *model = self.list[indexPath.row];
    cell.model = model;
    return cell;
}

@end
